package model;

import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.tree.DefaultMutableTreeNode;

public class Host {
	private String ipAddress, name;
	private ArrayList<Scan> scanHistory;
	public Host(String ipAddress){
		this.ipAddress = ipAddress;
		scanHistory = new ArrayList<Scan>();
		
	}
	public String getIPAddress(){
		return ipAddress;
	}
	public PortEntry[] getUniquePorts(){
		//returns all of the unique ports and services record from this host
		LinkedList<PortEntry> uniquePorts = new LinkedList<PortEntry>();
		PortEntry[] scanPorts;
		for (Scan scan : scanHistory){
			scanPorts = scan.getPorts();			
			scanPort:
			for (PortEntry scanPort : scanPorts){
				for (PortEntry uniquePort : uniquePorts){
					if(uniquePort.PortEntryEquals(scanPort)){
						break scanPort;
					}
				}
				
				// if scanPort is not in the list of unique ports, add
				
				//adds the ports in order based on the port number
				int i = 0;
				while (i<uniquePorts.size()){
					if (uniquePorts.get(i).getPortNumber()<scanPort.getPortNumber()){
						break;
					}
					i++;
				}
				uniquePorts.add(i, scanPort);
			}
			
		}
		return uniquePorts.toArray(new PortEntry[0]);
	}
	public boolean checkForService(String service){
		PortEntry[] ports = getUniquePorts();
		for (PortEntry portEntry: ports){
			if (portEntry.getService().equals(service)){
				return true;
			}
		}
		return false;
	}
	public boolean isIPLessThan(String arg){
		//handles ip4 addresses only
		String[] hostTokens = IPSplit(this.getIPAddress());
		String[] argTokens = IPSplit(arg);
		
		int hostInt, argInt;
		if (hostTokens.length==4&&argTokens.length==4){
			for (int i = 0; i<4; i++){
				hostInt = Integer.parseInt(hostTokens[i]);
				argInt = Integer.parseInt(argTokens[i]);
				if (hostInt>argInt){
					return false;
				}
			}
			return true;
		}
		else {
			System.out.println(this.getIPAddress() + " or "+ arg+" not properly formatted");
			
		}
		return false;
	}
	private String[] IPSplit(String toSplit){
		ArrayList<String> octets = new ArrayList<String>();
		
		int start = 0;
		int end = 0;
		
		
		while (end<toSplit.length()){
			if (toSplit.charAt(end)== '.'&& start<end){
				octets.add(toSplit.substring(start, end));
				start =end+1;
			}
			end++;
		}
		if (start<end){
		octets.add(toSplit.substring(start, end));
		}
		
		return octets.toArray(new String[0]);
	}
	public void addScan(Scan newScan){
		scanHistory.add(newScan);
	}
	public Scan[] getScanHistory(){
		return scanHistory.toArray(new Scan[0]);
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public LinkedList<String> getHostInfo() {
		LinkedList<String> hostInfo = new LinkedList<String>();
		hostInfo.add(ipAddress);
		return hostInfo;
	}
	public DefaultMutableTreeNode getTreeNode(){
		DefaultMutableTreeNode hostNode = new DefaultMutableTreeNode(ipAddress);
		
		PortEntry[] ports = getUniquePorts();
		for (PortEntry port: ports){
			hostNode.insert(new DefaultMutableTreeNode(port.getPortNumber()+" "+port.getService()), 0);
		}
		return hostNode;
	}
}
